﻿package dut.ir.lxb.weibo.db;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import dut.ir.lxb.weibo.bean.UserBean;

public class UserInfoDao
{
	private static UserInfoDao ud;
	
	private UserInfoDao()
	{
		
	}
	
	public static UserInfoDao getInstance()
	{
		if(ud == null)
		{
			ud = new UserInfoDao();
			return ud;
		}
		else
			return ud;
	}
	
	public boolean hasRecord(UserBean wb)
	{
		if(wb == null)
			return true;
		UserBean checkWb = getUserByUid(wb.getUid());
		if(checkWb == null)
			return false;
		return true;
	}
	
	public boolean deleteUser(String uid)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "delete from T_UserInfo where uid=?";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, uid);
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} 
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
		
	}
	
	
	/**
	 * 插入一个用户信息
	 * @param ub
	 * @return
	 */
	public boolean insertUser(UserBean ub)
	{
		if(ub == null || ub.getNickName().trim().equals("") || ub.getUid().trim().equals(""))
			return true;
		if(hasRecord(ub))
			deleteUser(ub.getUid());
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "insert into T_UserInfo (uid,nickname,ismember,isapproved,approveinfo,level,fans,follow,weibo,location,sex,sexorientation,martitalstatus,birthday,bloodtype,blog,site,introduction,createtime,email,qq,msn,company,tags,headpicture,educationinfo,isofficial) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, ub.getUid());
			ps.setString(2, ub.getNickName());
			ps.setBoolean(3, ub.isMember());
			ps.setBoolean(4, ub.isApproved());
			ps.setString(5, ub.getApproveInfo());
			ps.setInt(6, ub.getLevel());
			ps.setInt(7, ub.getFans());
			ps.setInt(8, ub.getFollow());
			ps.setInt(9, ub.getWeibo());
			ps.setString(10, ub.getLocation());
			ps.setString(11, ub.getSex());
			ps.setString(12, ub.getSexOrientation());
			ps.setString(13, ub.getMaritalStatus());
			ps.setString(14, ub.getBirth());
			ps.setString(15, ub.getBloodType());
			ps.setString(16, ub.getBlog());
			ps.setString(17, ub.getSite());
			ps.setString(18, ub.getIntroduction());
			
			
			Date currentDate = new Date(new java.util.Date().getTime());
			try
			{
				if(ub.getCreateTime() != null && ub.getCreateTime().trim().length() != 0)
				{
					java.util.Date utilDate = new SimpleDateFormat("yyyy-MM-DD").parse(ub.getCreateTime());
					currentDate = new Date(utilDate.getTime());
				}
				ps.setDate(19, currentDate);
			}
			catch (ParseException e)
			{
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}
			ps.setString(20, ub.getEmail());
			ps.setString(21, ub.getQq());
			ps.setString(22, ub.getMsn());
			ps.setString(23, ub.getCompany());
			ps.setString(24, ub.getTags());
			ps.setString(25, ub.getHeadPicture());
			ps.setString(26, ub.getEducationInfo());
			ps.setBoolean(27, ub.isOfficial());
			
			boolean flag = ps.execute();
			ps.close();
			return flag;
			
		}
		catch (SQLException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		} 
		catch(Exception e)
		{
			e.printStackTrace();
			return false;
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
	}
	
	public List<UserBean> getUserListByNickname(String nickname)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_UserInfo where nickname like ?";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, "%" + nickname + "%");
			List<UserBean> ubList = new ArrayList<UserBean>();
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				UserBean ub = new UserBean();
				
				ub.setUid(rs.getString("uid"));
				ub.setNickName(rs.getString("nickname"));
				ub.setMember(rs.getBoolean("ismember"));
				ub.setApproved(rs.getBoolean("isapproved"));
				ub.setApproveInfo(rs.getString("approveinfo"));
				ub.setLevel(rs.getInt("level"));
				ub.setFans(rs.getInt("fans"));
				ub.setFollow(rs.getInt("follow"));
				ub.setWeibo(rs.getInt("weibo"));
				ub.setLocation(rs.getString("location"));
				ub.setSex(rs.getString("sex"));
				ub.setSexOrientation(rs.getString("sexorientation"));
				ub.setMaritalStatus(rs.getString("martitalstatus"));
				ub.setBirth(rs.getString("birthday"));
				ub.setBloodType(rs.getString("bloodtype"));
				ub.setBlog(rs.getString("blog"));
				ub.setSite(rs.getString("site"));
				ub.setIntroduction(rs.getString("introduction"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				ub.setCreateTime(df.format(rs.getDate("createtime")));
				ub.setEmail(rs.getString("email"));
				ub.setQq(rs.getString("qq"));
				ub.setMsn(rs.getString("msn"));
				ub.setCompany(rs.getString("company"));
				ub.setTags(rs.getString("tags"));
				ub.setHeadPicture(rs.getString("headpicture"));
				ub.setEducationInfo(rs.getString("educationinfo"));
				ub.setOfficial(rs.getBoolean("isofficial"));
				
				ubList.add(ub);
			}
			
			rs.close();
			ps.close();
			return ubList;
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	/**
	 * 查找一个用户
	 * @return
	 */
	public UserBean getUserByUid(String uid)
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_UserInfo where uid=?";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			ps.setString(1, uid);
			ResultSet rs = ps.executeQuery();
			UserBean ub = new UserBean();
			if(rs.next())
			{
				
				
				ub.setUid(rs.getString("uid"));
				ub.setNickName(rs.getString("nickname"));
				ub.setMember(rs.getBoolean("ismember"));
				ub.setApproved(rs.getBoolean("isapproved"));
				ub.setApproveInfo(rs.getString("approveinfo"));
				ub.setLevel(rs.getInt("level"));
				ub.setFans(rs.getInt("fans"));
				ub.setFollow(rs.getInt("follow"));
				ub.setWeibo(rs.getInt("weibo"));
				ub.setLocation(rs.getString("location"));
				ub.setSex(rs.getString("sex"));
				ub.setSexOrientation(rs.getString("sexorientation"));
				ub.setMaritalStatus(rs.getString("martitalstatus"));
				ub.setBirth(rs.getString("birthday"));
				ub.setBloodType(rs.getString("bloodtype"));
				ub.setBlog(rs.getString("blog"));
				ub.setSite(rs.getString("site"));
				ub.setIntroduction(rs.getString("introduction"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				ub.setCreateTime(df.format(rs.getDate("createtime")));
				ub.setEmail(rs.getString("email"));
				ub.setQq(rs.getString("qq"));
				ub.setMsn(rs.getString("msn"));
				ub.setCompany(rs.getString("company"));
				ub.setTags(rs.getString("tags"));
				ub.setHeadPicture(rs.getString("headpicture"));
				ub.setEducationInfo(rs.getString("educationinfo"));
				ub.setOfficial(rs.getBoolean("isofficial"));
				
			}
			
			rs.close();
			ps.close();
			
			return ub;
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
		return null;
	}
	
	
	public List<UserBean> getTestUserList()
	{
		DBConnectionManager connectionMan = DBConnectionManager.getInstance();
		Connection conn = null;
		try
		{
			conn = connectionMan.getConnection();
			String sqlStr = "select * from T_UserInfo where nickname=''";
			PreparedStatement ps = conn.prepareStatement(sqlStr);
			
			//填充参数
			List<UserBean> ubList = new ArrayList<UserBean>();
			UserBean test = new UserBean();
			test.setUid("1849026987");
			ubList.add(test);
			
			ResultSet rs = ps.executeQuery();
			while(rs.next())
			{
				UserBean ub = new UserBean();
				
				ub.setUid(rs.getString("uid"));
				ub.setNickName(rs.getString("nickname"));
				ub.setMember(rs.getBoolean("ismember"));
				ub.setApproved(rs.getBoolean("isapproved"));
				ub.setApproveInfo(rs.getString("approveinfo"));
				ub.setLevel(rs.getInt("level"));
				ub.setFans(rs.getInt("fans"));
				ub.setFollow(rs.getInt("follow"));
				ub.setWeibo(rs.getInt("weibo"));
				ub.setLocation(rs.getString("location"));
				ub.setSex(rs.getString("sex"));
				ub.setSexOrientation(rs.getString("sexorientation"));
				ub.setMaritalStatus(rs.getString("martitalstatus"));
				ub.setBirth(rs.getString("birthday"));
				ub.setBloodType(rs.getString("bloodtype"));
				ub.setBlog(rs.getString("blog"));
				ub.setSite(rs.getString("site"));
				ub.setIntroduction(rs.getString("introduction"));
				SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
				ub.setCreateTime(df.format(rs.getDate("createtime")));
				ub.setEmail(rs.getString("email"));
				ub.setQq(rs.getString("qq"));
				ub.setMsn(rs.getString("msn"));
				ub.setCompany(rs.getString("company"));
				ub.setTags(rs.getString("tags"));
				ub.setHeadPicture(rs.getString("headpicture"));
				ub.setEducationInfo(rs.getString("educationinfo"));
				ub.setOfficial(rs.getBoolean("isofficial"));
				
				ubList.add(ub);
			}
			
			rs.close();
			ps.close();
			return ubList;
			
		}
		catch (SQLException e)
		{
			e.printStackTrace();
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
		finally
		{
			connectionMan.freeConnection(conn);
		}
		
		return null;
		
		
	}
	
	
	
	
	
}
